﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DevExpress.Xpf.LayoutControl;
using System.Data;

namespace nLearning.UC
{
    /// <summary>
    /// Interaction logic for Course.xaml
    /// </summary>
    public partial class Course : UserControl
    {
        public Course()
        {
            InitializeComponent();
        }

        List<string> generatedGroups = new List<string>();
        
        private void ApplicationTile(TileLayoutControl tileLayout)
        {
            #region Tile user
            string userName = Warehouse.USER_DATA.Tables[0].Rows[0][1].ToString();
            string userCourse = Warehouse.USER_DATA.Tables[0].Rows[0][2].ToString();
            string userInfo = string.Format("{0}\n{1}", userName, userCourse);

            Tile infoTile = new Tile() { Header = userInfo, Name = "tInfo", Size = TileSize.Large };
            string navGroup = "Application";
            infoTile.SetValue(TileLayoutControl.GroupHeaderProperty, navGroup);

            #region add image to tile
            Image tileImag = new Image();
            BitmapImage imag = new BitmapImage();
            Grid gr = new Grid();
            imag.BeginInit();
            imag.UriSource = new Uri("pack://application:,,,/nLearning;component/Images/Icons/info-7-128.png");
            imag.EndInit();
            tileImag.Source = imag;
            tileImag.Stretch = Stretch.None;
            tileImag.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            tileImag.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            infoTile.Content = tileImag;
            #endregion

            if (!generatedGroups.Contains(navGroup))
            {
                infoTile.SetValue(TileLayoutControl.IsFlowBreakProperty, true);
                generatedGroups.Add(navGroup);
            }
            tileLayout.Children.Add(infoTile);
            RegisterName(infoTile.Name, infoTile);
            #endregion

            #region Tile Back
            Tile backTile = new Tile() { Header = "", Name = "tBack", Size = TileSize.Small };
            backTile.SetValue(TileLayoutControl.GroupHeaderProperty, navGroup);

            #region add image to tile
            Image tileBackImag = new Image();
            BitmapImage Bimag = new BitmapImage();
            Bimag.BeginInit();
            Bimag.UriSource = new Uri("pack://application:,,,/nLearning;component/Images/Icons/back-128.png");
            Bimag.EndInit();
            tileBackImag.Source = Bimag;
            tileBackImag.Stretch = Stretch.None;
            backTile.Content = tileBackImag;
            #endregion

            if (!generatedGroups.Contains(navGroup))
            {
                backTile.SetValue(TileLayoutControl.IsFlowBreakProperty, true);
                generatedGroups.Add(navGroup);
            }
            tileLayout.Children.Add(backTile);
            RegisterName(backTile.Name, backTile);
            #endregion

            #region Tile exit
            Tile exitTile = new Tile() { Header = "", Name = "tExit", Size = TileSize.Small };
            exitTile.SetValue(TileLayoutControl.GroupHeaderProperty, navGroup);
            exitTile.Click += new EventHandler(exitTile_Click);

            #region add image to tile
            Image tileExitImag = new Image();
            BitmapImage imagExit = new BitmapImage();
            imagExit.BeginInit();
            imagExit.UriSource = new Uri("pack://application:,,,/nLearning;component/Images/Icons/exit-4-128.png");
            imagExit.EndInit();
            tileExitImag.Source = imagExit;
            tileExitImag.Stretch = Stretch.None;
            exitTile.Content = tileExitImag;
            #endregion

            if (!generatedGroups.Contains(navGroup))
            {
                exitTile.SetValue(TileLayoutControl.IsFlowBreakProperty, true);
                generatedGroups.Add(navGroup);
            }
            tileLayout.Children.Add(exitTile);
            RegisterName(exitTile.Name, exitTile);
            #endregion
        }

        private void GenerateTile()
        {
            #region Clear old tile
            foreach (Tile tile in tileLayoutControl1.Children.OfType<Tile>())
                UnregisterName(tile.Name);

            tileLayoutControl1.Children.Clear();
            #endregion

            #region Generate from data
            foreach (DataRow dr in Warehouse.USER_COURSE.Tables[0].Rows)
            {
                Warehouse.LECTURE_INFO info = new Warehouse.LECTURE_INFO();
                info.course_id = dr["CourseID"].ToString();
                info.lecture_id = dr["LectureID"].ToString();
                info.lecture_name = dr["LectureName"].ToString();
                info.course_name = dr["CourseName"].ToString();
                TileGenerater(info);
            }
            #endregion

            ApplicationTile(tileLayoutControl1);
        }

        private void TileGenerater(Warehouse.LECTURE_INFO infoTag)
        {
            try
            {
                Random ed = new Random();
                int rdName = ed.Next(20001, 99999);
                string eman = "A" + rdName + "A";

                Tile newTile = new Tile() { Header = infoTag.lecture_name, Name = eman + infoTag.lecture_id, Size = TileSize.Large, FontSize=16.0 };

                newTile.SetValue(TileLayoutControl.GroupHeaderProperty, infoTag.course_name);
                newTile.Tag = infoTag;
                newTile.Click += new EventHandler(newTile_Click);

                if (!generatedGroups.Contains(infoTag.course_name))
                {
                    newTile.SetValue(TileLayoutControl.IsFlowBreakProperty, true);
                    generatedGroups.Add(infoTag.course_name);
                }
                tileLayoutControl1.Children.Add(newTile);
                RegisterName(newTile.Name, newTile);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void newTile_Click(object sender, EventArgs e)
        {
            Tile tSender = (Tile)sender;
            UC.Lecture lecture = new Lecture();
            lecture.LectureInfo = (Warehouse.LECTURE_INFO) tSender.Tag;
            Warehouse.lecture_info = (Warehouse.LECTURE_INFO)tSender.Tag;
            WpfPageTransitions.PageTransition transition = new WpfPageTransitions.PageTransition();
            transition.TransitionType = WpfPageTransitions.PageTransitionType.Slide;
            transition.ShowPage(lecture);
            Window.GetWindow(this).Content = transition;
            //throw new NotImplementedException();
        }
        
        void exitTile_Click(object sender, EventArgs e)
        {
            if (Message.Confirm(cString.ID("logout")))
            {
                UC.Login log = new Login();
                WpfPageTransitions.PageTransition transition = new WpfPageTransitions.PageTransition();
                transition.TransitionType = WpfPageTransitions.PageTransitionType.Slide;
                transition.ShowPage(log);
                Window.GetWindow(this).Content = transition;
            }
            //throw new NotImplementedException();
        }

        private void CourseLoad(object sender, RoutedEventArgs e)
        {
            GenerateTile();
        }
    }
}
